package com.tduck.cloud.poster.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.common.util.SecurityUtils;
import com.tduck.cloud.common.util.ShortIdUtils;
import com.tduck.cloud.poster.entity.PosterEntity;
import com.tduck.cloud.poster.service.PosterService;
import com.tduck.cloud.poster.util.ImageCombinerBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;


/**
 * 海报管理
 * @author : tduck
 * @since  :  2023/02/01 14:43
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/poster")
public class PosterController {

    private final PosterService posterService;

    /**
     * 查询海报列表
     */
    @GetMapping("/page")
    public Result<Page<PosterEntity>> queryPage(Page<PosterEntity> page, PosterEntity poster) {
        poster.setUserId(SecurityUtils.getUserId());
        LambdaQueryWrapper<PosterEntity> queryWrapper = Wrappers.<PosterEntity>lambdaQuery().like(StrUtil.isNotBlank(poster.getName()), PosterEntity::getName, poster.getName());
        Page<PosterEntity> pageResult = posterService.page(page, queryWrapper);
        return Result.success(pageResult);
    }


    /**
     * 获取海报详细信息
     */
    @GetMapping(value = "/{id}")
    public Result<PosterEntity> getInfo(@PathVariable("id") Long id) {
        return Result.success(posterService.getById(id));
    }

    /**
     * 获取报仪盘详细信息
     */
    @GetMapping(value = "/key/{key}")
    public Result<PosterEntity> getInfo(@PathVariable("key") String key) {
        return Result.success(posterService.getOne(Wrappers.<PosterEntity>lambdaQuery().eq(PosterEntity::getPosterKey, key)));
    }

    /**
     * 新增海报
     */
    @PostMapping("save")
    public Result<PosterEntity> save(@RequestBody PosterEntity poster) {
        poster.setUserId(SecurityUtils.getUserId());
        poster.setPosterKey(ShortIdUtils.genId());
        boolean save = posterService.save(poster);
        return Result.success(poster);
    }


    /**
     * 修改海报
     */
    @PostMapping("update")
    public Result<PosterEntity> update(@RequestBody PosterEntity poster) {
        posterService.updateById(poster);
        return Result.success(poster);
    }

    /**
     * 删除海报
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(posterService.removeById(id));
    }


    /**
     * 生成海报
     */
    @PostMapping("/generate")
    public Result<PosterEntity> generate(@RequestBody PosterEntity poster) {
        try {
            ImageCombinerBuilder.build(poster);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("生成海报失败");
        }
        return Result.success(null);
    }


    /**
     * 预览海报
     */
    @PostMapping("/preview")
    public Result<String> preview(@RequestBody PosterEntity poster) {
        try {
            return Result.success(ImageCombinerBuilder.uploadImageUrl(poster.getPosterKey(), ImageCombinerBuilder.build(poster).getCombinedImageStream()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("生成海报失败");
        }
    }

}
